package com.okami.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: TanJingyu
 * @create: 2020-12-17 15:42
 **/
@RestController
@RequestMapping("/executor/jdk")
public class JdkExecutorController {
    private final ThreadPoolExecutor threadPoolExecutor;

    public JdkExecutorController(ThreadPoolExecutor threadPoolExecutor) {
        this.threadPoolExecutor = threadPoolExecutor;
    }

    @GetMapping
    public String executor() {
        System.out.println(threadPoolExecutor);
        System.out.println(threadPoolExecutor.getQueue());
        return "success";
    }

    @GetMapping("/execut")
    public String execut() throws ExecutionException, InterruptedException {
        Future<?> resultFuture = threadPoolExecutor.submit(() -> {
            try {
                System.out.println("开始睡觉--------" + Thread.currentThread().getName());
                TimeUnit.MINUTES.sleep(30);
                System.out.println("睡醒了............." + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(resultFuture.get());
        return "success";
    }
}
